package com.zoffy.ybuhelper;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.zoffy.ybuhelper.fragment.CategoryControlFragment;
import com.zoffy.ybuhelper.fragment.MenuControlFragment;
import com.zoffy.ybuhelper.fragment.OriginControlFragment;
import com.zoffy.ybuhelper.fragment.UpdateFragment;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class EatControlActivity extends AppCompatActivity {
    private List<UpdateFragment> fragments;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigator_base);
        setupViewPager();
    }

    public void setupViewPager() {
        fragments = new ArrayList<>();
        fragments.add(new MenuControlFragment("menu", "菜品管理"));
        fragments.add(new OriginControlFragment("origin", "商户管理"));
        fragments.add(new CategoryControlFragment("category", "类别管理"));
        ViewPager2 viewPager2 = findViewById(R.id.menu_pager2);
        viewPager2.setUserInputEnabled(false);
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @NotNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });


        BottomNavigationView bottomNavigationView = findViewById(R.id.btm_nav);
        bottomNavigationView.inflateMenu(R.menu.eat_control_menu);
        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_control:
                        viewPager2.setCurrentItem(0);
                        break;
                    case R.id.origin_control:
                        viewPager2.setCurrentItem(1);
                        break;
                    case R.id.category_control:
                        viewPager2.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });
    }
}
